package com.yuexin.project.system.group.controller;

import java.util.List;

import com.google.common.collect.Lists;
import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.project.system.group.domain.Groups;
import com.yuexin.project.system.group.domain.GroupsList;
import com.yuexin.project.system.group.formbean.GroupsListFormBean;
import com.yuexin.project.system.group.service.IGroupsService;
import com.yuexin.project.system.user.domain.User;
import com.yuexin.project.system.user.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.yuexin.framework.aspectj.lang.annotation.Log;
import com.yuexin.framework.aspectj.lang.enums.BusinessType;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.framework.web.page.TableDataInfo;
import com.yuexin.framework.web.domain.AjaxResult;

/**
 * 人员组 信息操作处理
 *
 * @author zoncont.ahanu
 * @date 2019-04-30
 */
@Controller
@RequestMapping("/system/group")
@Api(value = "人员组 信息操作处理", description = "人员组 信息操作处理")
public class GroupsController extends BaseController {
    private String prefix = "system/group";

    @Autowired
    private IGroupsService groupsService;
    @Autowired
    private UserMapper userMapper;

    @RequiresPermissions("system:group:view")
    @GetMapping()
    @ApiOperation("人员组页面")
    public String groups() {
        return prefix + "/groups";
    }

    /**
     * 查询人员组列表
     */
    @RequiresPermissions("system:group:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("查询人员组列表")
    public TableDataInfo list(GroupsListFormBean form) {
        form.setCompanyId(ShiroUtils.getCompanyId());
        startPage();
        List<GroupsList> list = groupsService.selectGroupsList(form);
        return getDataTable(list);
    }

    /**
     * 新增人员组
     */
    @GetMapping("/add")
    @ApiOperation("新增人员组")
    public String add(ModelMap mmap) {
        mmap.put("groupUsers", selectUsersByGroupId(null, ShiroUtils.getCompanyId()));
        return prefix + "/add";
    }

    public List<User> selectUsersByGroupId(Integer groupId, String companyId) {
        List<User> userPosts = groupId != null ? userMapper.selectUsersByGroupId(groupId) : null;
        List<Long> users = Lists.newArrayList();
        if (userPosts != null && !userPosts.isEmpty()) {
            userPosts.stream().forEach(item -> users.add(item.getUserId()));
        }
        List<User> posts = userMapper.selectUserAllByCompanyId(companyId);
        if (posts != null && !posts.isEmpty()) {
            posts.stream().forEach(item -> {
                if (users.contains(item.getUserId())) {
                    item.setFlag(true);
                }
            });
        }
        return posts;
    }

    /**
     * 新增保存人员组
     */
    @RequiresPermissions("system:group:add")
    @Log(title = "人员组", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("新增保存人员组")
    public AjaxResult addSave(Groups groups) {
        groups.setCompanyId(ShiroUtils.getCompanyId());
        return toAjax(groupsService.insertGroups(groups));
    }

    /**
     * 修改人员组
     */
    @GetMapping("/edit/{id}")
    @ApiOperation("修改人员组")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        Groups groups = groupsService.selectGroupsById(id);
        mmap.put("groups", groups);
        mmap.put("groupUsers", selectUsersByGroupId(id, ShiroUtils.getCompanyId()));
        return prefix + "/edit";
    }

    /**
     * 修改保存人员组
     */
    @RequiresPermissions("system:group:edit")
    @Log(title = "人员组", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation("修改保存人员组")
    public AjaxResult editSave(Groups groups) {
        return toAjax(groupsService.updateGroups(groups));
    }

    /**
     * 删除人员组
     */
    @RequiresPermissions("system:group:remove")
    @Log(title = "人员组", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    @ApiOperation("删除人员组")
    public AjaxResult remove(String ids) {
        return toAjax(groupsService.deleteGroupsByIds(ids));
    }

}
